package com.lzj.demo.design.proxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 实现对MyBatis代理类的定义
 * */
@Slf4j
public class MapperFactoryBean<T> implements FactoryBean {

    private Class<T> mapperInterface;

    public MapperFactoryBean(Class<T> mapperInterface){
        this.mapperInterface = mapperInterface;

    }

    @Override
    public Object getObject() throws Exception {
        InvocationHandler handler = (proxy,method,args)->{
            Select select = method.getAnnotation(Select.class);
            log.info("SQL:{}",select.value().replace("#{uid}",args[0].toString()));
            return args[0];
        };
        return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class[]{mapperInterface},handler);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
